﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using System.Linq;
using System.Text;

namespace Survival.Animals
{
    public abstract partial class Animal
    {
        public class HerdMover : IAnimalMover
        {
            //TODO: make this read scripts to get speeds.
            private Vector2 position;
            private Vector2 velocity;
            private Vector2 targetPosition;
            readonly private float maxMoveSpeed;

            public Vector2 Position { get { return position; } }
            public float Rotation { get { return -(float)Math.Atan2(velocity.X, velocity.Y); } }

            public HerdMover(Vector2 position, float movespeed)
            {
                this.position = position;
                this.maxMoveSpeed = movespeed;
                targetPosition = position + Vector2.One;
            }

            public void UpdateMovement(GameTime gameTime)
            {
                //TODO: Should eventually get rid of this literal
                if (!IsNearTarget(targetPosition, 5))
                {
                    velocity = Vector2.Normalize(targetPosition - position);
                    position += (velocity * (float)gameTime.ElapsedGameTime.TotalSeconds) * maxMoveSpeed;
                }
            }

            public void MoveTo(Vector2 targetPosition)
            {
                this.targetPosition = targetPosition;
            }

            bool IsNearTarget(Vector2 target, float tolerance)
            {
                if (Vector2.DistanceSquared(position, targetPosition) < (tolerance * tolerance))
                    return true;
                return false;
            }

        }
    }
}
